"""
cookie

- 解释：用来保持服务器和浏览器交互的状态的，由服务器设置，存储在浏览器
- 作用：用来做广告推送
- cookie的设置和获取
    - 设置cookie：response.set_cookie(key, value, max_age)
        - max_age: 表示cookie在浏览器的存储时间，单位：秒
    - 获取cookie：request.cookies.get("key") 
"""


from flask import Flask, make_response, request

app = Flask(__name__)


# 设置cookie
@app.route("/set_cookie")
def set_cookie():
    # 调用 make_response() 方法获取响应体对象
    response = make_response()

    # 设置 cookie
    response.set_cookie("computer", "lenovo")

    # 设置 cookie 20秒后过期
    response.set_cookie("age", "23", 20)

    return response


# 设置cookie
@app.route("/get_cookie")
def get_cookie():
    # 获取 cookie
    name = request.cookies.get("computer")
    age = request.cookies.get("age")

    # 返回
    return f"name is {name}, age is {age}"


@app.route("/")
def helloworld():
    return "hello world Flask"


if __name__ == "__main__":
    app.run(debug=True)
